package com.ciaojian.client.strategy.discount;

import com.ciaojian.client.enums.DiscountTypeEnum;
import com.ciaojian.client.factory.DiscountCalculatorFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 满减优惠
 *
 * @author oxiny
 */
@Component
public class MJCouponCalculator implements DiscountCalculator<Map<String, Integer>> {
    /**
     * 满减优惠计算
     *
     * @param couponInfo 满减门槛 x, 满减金额 y
     * @param skuPrice   原金额
     * @return 判断满足x元后-n元，否则不减
     */
    @Override
    public Integer calculate(Map<String, Integer> couponInfo, Integer skuPrice) {
        Integer x = couponInfo.get("x");
        Integer y = couponInfo.get("y");

        // 小于商品金额条件的，直接返回商品原价
        if (skuPrice.compareTo(x) < 0) {
            return skuPrice;
        }
        // 减去优惠金额
        return skuPrice - y;
    }

    @Override
    public DiscountTypeEnum getType() {
        return DiscountTypeEnum.MAN_JIAN;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DiscountCalculatorFactory.register(this);
    }
}
